package com.xbdun.dao.validatebmp.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Options.FlushCachePolicy;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import com.xbdun.dao.validatebmp.entity.EcUploadFileDetail;
import com.xbdun.dao.validatebmp.entity.EcUploadFileInfo;
import com.xbdun.dao.validatebmp.provider.UploadFileSqlProvider;

/**
 * 文件上传操作DAO
 */
@Mapper
public interface UploadFileMapper {
	/**
	 * 保存文件信息
	 * @param uploadFileInfo 
	 * @return 返回上传文件Id
	 * @throws UploadFileError
	 */
	@InsertProvider(type=UploadFileSqlProvider.class,method="saveFileInfoSql")
	//@SelectKey(statement="/*master*/SELECT LAST_INSERT_ID() AS ID", before = false, keyProperty = "fileInfo.uploadFileId", resultType = Long.class)
	public int saveFileInfo(@Param("fileInfo") EcUploadFileInfo uploadFileInfo) throws Exception;
	/**
	 * 保存文件详情信息
	 * @param uploadFileId 上传文件Id
	 * @param fileDetails  详情信息
	 */
	@InsertProvider(type=UploadFileSqlProvider.class,method="saveFileDetailSql")
	public void saveFileDetail(@Param("fileDetails") List<EcUploadFileDetail> fileDetails) throws Exception;
	/**
	 * 获取上传文件详情信息
	 * @param uploadFileId 上传文件Id
	 * @param fileFormat 规格   等于-1时不加入查询条件
	 * @return
	 */
	
	@SelectProvider(type=UploadFileSqlProvider.class,method="queryFileDetailSql")
	public List<EcUploadFileDetail> queryFileDetail(@Param("uploadFileId")long uploadFileId, @Param("fileFormat")int fileFormat) throws Exception;
	/**
	 * 获取上传文件详情信息
	 * @param ids 上传文件Id字符串，多个Id用英文逗号隔开
	 * @param fileFormat 规格 等于-1时不加入查询条件
	 * @return
	 */
	@Options(useCache = true, flushCache = FlushCachePolicy.FALSE, timeout = 10000)
	@SelectProvider(type=UploadFileSqlProvider.class,method="queryFileDetailsByIds")
	public List<EcUploadFileDetail> queryFileDetailsByIds(@Param("ids")String ids, @Param("fileFormat")int fileFormat) throws Exception;
	/**
	 * 逻辑删除文件信息
	 * @param uploadFileId 上传文件Id
	 */
	@Delete("UPDATE xbdun_upload_file_info a left JOIN xbdun_upload_file_detail b on a.uploadFileId=b.uploadFileId SET a.isDelete=1,b.isDelete=1 WHERE a.uploadFileId=#{uploadFileId}") 
	public int delFileInfo(@Param("uploadFileId")long uploadFileId) throws Exception;
	/**
	 * 逻辑删除文件信息 
	 * @param ids 上传文件Id字符串，多个Id用英文逗号隔开
	 * @return
	 */
	@DeleteProvider(type=UploadFileSqlProvider.class,method="delFileInfoByIdsSql")
	public int delFileInfoByIds(@Param("ids")String ids) throws Exception;
	/**
	 * 获取上传文件中大小（字节）
	 * @return
	 */
	@Options(useCache = true, flushCache = FlushCachePolicy.FALSE, timeout = 10000)
	@Select("SELECT SUM(size) size FROM xbdun_upload_file_detail")
	public long getTotalSize() throws Exception;
	/**
	 * 获取上传文件总数量 包括已经删除的文件
	 * @return
	 */
	@Options(useCache = true, flushCache = FlushCachePolicy.FALSE, timeout = 10000)
	@Select("SELECT COUNT(*) size FROM xbdun_upload_file_detail")
	public long getTotalQuantity() throws Exception;
	/**
	 * 获取已经删除的文件总数量
	 * @return
	 */
	@Options(useCache = true, flushCache = FlushCachePolicy.FALSE, timeout = 10000)
	@Select("SELECT COUNT(*) size FROM xbdun_upload_file_detail where isDelete=1")
	public long getTotalDeletedQuantity() throws Exception;
	
}
